<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array();
$types=get_permission_types('id','all');
foreach($types as $row){
	$tabs[]=array('op'=>'list','query'=>'type='.$row['id'],'title'=>$row['title']);
}
$tabs[]=array('op'=>'num','title'=>'各DIY个数价格设置');
$tabs[]=array('op'=>'setapp','title'=>'开启DIY功能价格设置');
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$type=intval($_RQ['type']);
	$group_id=intval($_RQ['group_id']);
	$return['params']['type']=$type;
	$return['params']['group_id']=$group_id;
	if(!$type){
		//系统功能
		$params=array('type'=>array('1','2','3'));
	}elseif(in_array($type,array('2','3','5'))){
		//手机站模版、pc站模版、小程序模版
		$params=array('uniacid'=>0);
	}else{
		$params=array('hide'=>0);
	}
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$fields=array('id','title','price','days','image');
	if(in_array($type,array('2','3','5'))){
		$fields[]='group_id';
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	$list=pdo_getslice($types[$type]['table'],$params,array($pindex,$psize),$total,$fields,'id',$orderby);
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：已经关闭的插件，这里不会显示！','delall'=>false,'edit'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'image','title'=>'功能图片','type'=>'image'),
			array('name'=>'title','title'=>'功能名称','type'=>'string'),
			array('name'=>'price','title'=>'功能价格','type'=>'string'),
			array('name'=>'days','title'=>'时间单位','type'=>'string'),
		),'action'=>array(
			array('op'=>'edit','title'=>'设置价格','type'=>'href','query'=>'type='.$type),
		)),
		'search_one'=>array('select'=>false,'add'=>false,'name'=>'title'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	$type=intval($_RQ['type']);
	$item=pdo_get($types[$type]['table'],array('id'=>$id),array('price','days','title','image'));
	$item['type']=$type;
	$item['id']=$id;
	$htmls=array(
		'type'=>'edit',
		'post'=>'set',
		'desc'=>'设置功能价格',
		'edit'=>array(
			array('type'=>'string','title'=>'标题','name'=>'title'),
			array('type'=>'image','title'=>'图片','name'=>'image'),
			array('type'=>'string','title'=>'价格','name'=>'price','help'=>'设置价格，填写正整数或0'),
			array('type'=>'string','title'=>'收费周期','name'=>'days','help'=>'收费周期，填写正整数（单位：天）'),
		)
	);
	$return['item']=$item;
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('请选择应用！','error');
	}
	$type=intval($post['type']);
	
	if($post['days'] && !$post['price']){
		exi('请填写：价格！','error');
	}
	if(!$post['days'] && $post['price']){
		exi('请填写：天数！','error');
	}
	$data=array(
			'price'=>intval($post['price']),
			'days'=>intval($post['days']),
			'title'=>$post['title'],
			'image'=>$post['image'],
	);
	pdo_update($types[$type]['table'],$data,array('id'=>$id));
	exi('设置成功！','','goback');
}
if($_RQ['op']=='num'){
	if($_RQ['postdata']){
		$post=get_postdata();
		core_setting('num_price',$post);
		exi('设置成功！');
	}
	$item=core_setting('num_price');
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'设置价格后，diy官网将支持数量购买',
		'edit'=>array(
			array('type'=>'string','name'=>'wechat','title'=>'每个公众号价格（单位：元）'),
			array('type'=>'string','name'=>'wxapp','title'=>'每个微信小程序价格（单位：元）'),
			array('type'=>'string','name'=>'aliapp','title'=>'每个支付宝小程序价格（单位：元）'),
			array('type'=>'string','name'=>'baiduapp','title'=>'每个百度小程序价格（单位：元）'),
			array('type'=>'string','name'=>'ttapp','title'=>'每个头条/抖音价格（单位：元）'),
			array('type'=>'string','name'=>'qqapp','title'=>'每个QQ小程序价格（单位：元）'),
			array('type'=>'string','name'=>'mobile','title'=>'每个diy官网价格（单位：元）'),
			array('type'=>'string','name'=>'pc','title'=>'每个PC官网价格（单位：元）'),
			array('type'=>'string','name'=>'attachment','title'=>'附件容量价格（单位：元/G）'),
		),
	);
	exi($return);
}
if($_RQ['op']=='setapp'){
	if($_RQ['postdata']){
		$post=get_postdata();
		core_setting('app_price',$post);
		exi('设置成功！');
	}
	$item=core_setting('app_price');
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'设置价格后，客户可以付费开启相关功能！',
		'edit'=>array(
			array('type'=>'string','name'=>'wxapp','title'=>'开启微信小程序价格（单位：元/年）'),
			array('type'=>'string','name'=>'aliapp','title'=>'开启支付宝小程序价格（单位：元/年）'),
			array('type'=>'string','name'=>'baiduapp','title'=>'每开启百度小程序价格（单位：元/年）'),
			array('type'=>'string','name'=>'ttapp','title'=>'开启头条/抖音小程序价格（单位：元/年）'),
			array('type'=>'string','name'=>'qqapp','title'=>'开启QQ小程序价格（单位：元/年）'),
			array('type'=>'string','name'=>'pc','title'=>'开启PC官网价格（单位：元/年）'),
			array('type'=>'string','name'=>'xiu','title'=>'开启场景秀价格（单位：元/年）'),
			array('type'=>'string','name'=>'h5game','title'=>'开启互动营销价格（单位：元/年）'),
		),
	);
	exi($return);
}